package  
{
	import as3isolib.graphics.IFill;
	import as3isolib.graphics.SolidColorFill;
	import as3isolib.graphics.Stroke;
	import com.smartfoxserver.v2.entities.variables.SFSRoomVariable;
	import com.smartfoxserver.v2.entities.variables.SFSUserVariable;
	import com.smartfoxserver.v2.requests.SetRoomVariablesRequest;
	/**
	 * ...
	 * @author Bryan Keiren & Sander van Katwijk
	 */
	public class Team 
	{
		static protected var m_TeamList:Vector.<Team> = new Vector.<Team>;
		static protected function registerTeam(a_Team:Team):void	{	m_TeamList.push(a_Team);							}
		static protected function unregisterTeam(a_Team:Team):void	{	m_TeamList.splice(m_TeamList.indexOf(a_Team), 1);	}
		static public function getTeams():Vector.<Team>				{	return m_TeamList;									}
		static public function getTeamByID(a_ID:int):Team			{	for each(var team:Team in m_TeamList)	
																		{	if (team.ID == a_ID)	return team;	}
																		return null;										 }
		
		static private var m_CurTeamID:int = 0;
		public var ID:int = 0;
		
		private var m_Players:Vector.<Player> = new Vector.<Player>();
		
		protected var m_Score:Number = 0;
		protected var m_LastScore:int = 0;
		public function getScore():Number					{	return m_Score;		}
		public function setScore(a_Score:Number):void		{	m_Score = a_Score;	}

		
		private var m_Stroke:Stroke;
		private var m_Fill:SolidColorFill;
		public function getStroke():Stroke					{	return m_Stroke;	}
		public function getFill():SolidColorFill			{	return m_Fill;		}
		public function showPlayerNames(bool:Boolean):void	{ for each(var player:Player in m_Players) { player.showName(bool); } }
		
		public function getPlayers():Vector.<Player>
		{
			return m_Players;
		}
		
		public function Team(a_Color:uint = Globals.DefaultTeamColor) 
		{
			ID = m_CurTeamID++;
			m_Stroke = new Stroke(1, a_Color);
			m_Fill = new SolidColorFill(a_Color, 1);
			registerTeam(this);
			Engine.printf("Created team " + ID);
		}
		public function destroy():void
		{
			unregisterTeam(this);
			
			// Get all flags for this team and set them to neutral.
			if (m_Players.length > 0)
			{
				Engine.printf("WARNING: Deleting team " + ID + " while there are still players in the team!");
			}
			
			var flagVarsArray:Array = [];
			for each (var flag:Flag in Flag.getFlags())
			{
				if (flag.getTeam().ID == ID)
				{
					flag.attemptCapture(Team.getDefaultTeam(), true);
					
					flagVarsArray.push( new SFSRoomVariable("node" + flag.ID + "owner", Team.getDefaultTeam().getID()) );
				}
			}
			MessageHandler.Get().m_SFS.send( new SetRoomVariablesRequest(flagVarsArray, Globals.Current_Game_Room) );
			
			delete this;
		}
		
		static public function destroyAllExternalTeams():void
		{
			var off:int = 0;
			while (m_TeamList.length > 2)
			{
				var team:Team = m_TeamList[off];
				if (team.getID() != LocalPlayer.Get().getTeam().getID() && team.getID() != Team.getDefaultTeam().getID())
					team.destroy();
				else
					off++;
			}
		}
		
		public function getID():int
		{
			return ID;
		}
		
		public function setID(a_ID:int):void
		{
			ID = a_ID;
			m_Fill.color = Globals.TeamColors[ID];
			m_Stroke.color = Globals.TeamColors[ID];
			for each (var plyr:Player in m_Players)
			{
				plyr.setTeamColor();
			}
			//Engine.printf("Setting team ID to " + ID + " and color to " + m_Fill.color);
		}
		
		private function setColor(a_Color:uint):void
		{
			m_Stroke.color = a_Color;
			m_Fill.color = a_Color;
		}
		
		static private var m_DefaultTeam:Team;
		static public function getDefaultTeam():Team
		{
			if (m_DefaultTeam == null)
			{
				m_DefaultTeam = new Team();
				m_DefaultTeam.setID( 0 );
			}
			return m_DefaultTeam;
		}
		
		public function logic():void
		{
			for each(var player:Player in m_Players)
			{
				player.logic();
			}
			
			if (this == LocalPlayer.Get().getTeam())
			{
				// update score
				var scoreToAdd:Number = 0;
				for each(var flag:Flag in Flag.getFlags())
				{
					if (flag.getTeam() == this)
					{
						scoreToAdd += Globals.Score_Per_Flag * Globals.Delta_Time_S;
					}
				}
				m_Score += scoreToAdd;
				if (Math.floor(m_Score) > m_LastScore + Globals.Score_Send_Interval)
				{
					var scoreArray:Array = [];
					scoreArray.push(new SFSUserVariable("score", m_Score));
					MessageHandler.Get().sendSetUserVariablesRequest(scoreArray);
					m_LastScore = Math.floor(m_Score);
				}
			}
		}
		public function addPlayer(a_Player:Player):void
		{
			//Engine.printf("Adding player " + a_Player.getName() + " to team " + ID);
			// Add the player to the list.
			m_Players.push(a_Player);
		}
		public function removePlayer(a_Player:Player):void
		{
			// Remove the player from the list.
			m_Players.splice(m_Players.indexOf(a_Player), 1);
		}
		public function getNumPlayers():int
		{
			return m_Players.length;
		}
	}

}